package com.nbgs.demo.controller;

import com.nbgs.demo.common.Result;
import com.nbgs.demo.service.CountOverdueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Timer;
import java.util.TimerTask;

@RestController
@Slf4j
@CrossOrigin("*")
@Api(tags = "滞纳金计费接口", description = "定时每天进行滞纳金的计算")
public class CountOverdueController implements ApplicationRunner {

    @Autowired
    private CountOverdueService countOverdueService;



    @PutMapping("updateOverdueInfo")
    @ApiOperation("执行计算费用的方法")
    private Result updateOverdueInfo(){

        countOverdueService.countOverdueInfo();

        return Result.success();
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {
        updateTimer();
    }

    @ApiOperation("服务器开启直接定时自动执行")
    private void updateTimer(){
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                updateOverdueInfo();
            }
        },0,24*60*60*1000);
    }
}
